package com.lei.service.impl;

import com.lei.dao.RoleDao;
import com.lei.dao.UserDao;
import com.lei.domain.Role;
import com.lei.domain.User;
import com.lei.service.UserService;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author Admin
 * @Date 2021/9/16 16:57
 * @Version 1.0
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public List<User> list() {
        //获得所有user信息
        List<User> userList=userDao.list();

        for (User user:userList) {
            //获得每个user的id
            long id=user.getId();
            //
            List<Role> roles=roleDao.findRoleByUserId(id);
            user.setRoles(roles);
        }

        return userList;
    }

    public void save(User user, long[] roleIds) {
        Long userId=userDao.save(user);
        userDao.saveUserRoleRel(userId,roleIds);
    }

    public void del(Long userId) {
        userDao.delUserRoleRel(userId);
        userDao.delUser(userId);
    }
}
